import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class TodoDbAdapter {

		// Database fields
		public static final String KEY_ROWID = "_id";
		public static final String KEY_CATEGORY = "category";
		public static final String KEY_SUMMARY = "summary";
		public static final String KEY_DESCRIPTION = "description";
		private static final String DATABASE_TABLE = "todo";
		private Context context;
		private SQLiteDatabase database;
		private TodoDatabaseHelper dbHelper;

		public TodoDbAdapter(Context context) {
			this.context = context;
		}

		public TodoDbAdapter open() throws SQLException {
			dbHelper = new TodoDatabaseHelper(context);
			database = dbHelper.getWritableDatabase();
			return this;
		}

		public void close() {
			dbHelper.close();
		}

		/**
		 * Create a new todo If the todo is successfully created return the new
		 * rowId for that note, otherwise return a -1 to indicate failure.
		 */
		public long createTodo(String category, String summary, String description) {
			ContentValues initialValues = createContentValues(category, summary,
					description);

			return database.insert(DATABASE_TABLE, null, initialValues);
		}

		/**
		 * Update the todo
		 */
		public boolean updateTodo(long rowId, String category, String summary,
				String description) {
			ContentValues updateValues = createContentValues(category, summary,
					description);

			return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
					+ rowId, null) > 0;
		}

		/**
		 * Deletes todo
		 */
		public boolean deleteTodo(long rowId) {
			return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
		}

		/**
		 * Return a Cursor over the list of all todo in the database
		 * 
		 * @return Cursor over all notes
		 */
		public Cursor fetchAllTodos() {
			return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
					KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null,
					null, null);
		}

		/**
		 * Return a Cursor positioned at the defined todo
		 */
		public Cursor fetchTodo(long rowId) throws SQLException {
			Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
					KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION },
					KEY_ROWID + "=" + rowId, null, null, null, null, null);
			if (mCursor != null) {
				mCursor.moveToFirst();
			}
			return mCursor;
		}

		private ContentValues createContentValues(String category, String summary,
				String description) {
			ContentValues values = new ContentValues();
			values.put(KEY_CATEGORY, category);
			values.put(KEY_SUMMARY, summary);
			values.put(KEY_DESCRIPTION, description);
			return values;
		}
	}